//
//  HospitalIntroductionViewController.m
//  hospitalVIP
//
//  Created by Xiaoyan on 12-12-8.
//  Copyright (c) 2012年 MEETStudio. All rights reserved.
//

#import "HospitalIntroductionViewController.h"

#import "UploadDataViewController.h"
#import "HealthDiaryViewController.h"
#import "BlogMainViewViewController.h"
#import "ForumMainViewViewController.h"
#import "SettingViewController.h"
#import "ChartViewController.h"
#import "CircleViewController.h"
#import "CustomNavigationBar.h"

#import "CalendarViewController.h"

@interface HospitalIntroductionViewController ()

@end

@implementation HospitalIntroductionViewController


- (void)dealloc{
    
    [myScrollView release];
    [myPageControl release];
    
    [_scrollView release];
    
    [myTimer invalidate];
    [myTimer release];
    myTimer = nil;
    
    [hosView release];
    hosView =  nil;
    
    [clientView release];
    clientView = nil;
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = YES;
    
    [super viewWillAppear:YES];
    
    int role = 2;
    
    
    if ([[NSUserDefaults standardUserDefaults] valueForKey:ROLE ] != nil) {
        role =   [[[NSUserDefaults standardUserDefaults] valueForKey:ROLE ] integerValue];

    }
    
      if (role == 2) {
          clientView.hidden = NO;
          [hosView removeFromSuperview];
          
    }
      else{
          
          clientView.hidden = YES;
          [_scrollView addSubview:hosView];
          [hosView setFrame:clientView.frame];
      }
    
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"胎心监护";
    // Do any additional setup after loading the view from its nib.
     [myScrollView setContentSize:CGSizeMake(5*320, myScrollView.frame.size.height)];
    myScrollView.frame = CGRectMake(0, 0, 320, 163);
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bg_pattern_wood" ofType:@"png"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:path]];
    
    myScrollView.pagingEnabled = YES;
	myScrollView.showsHorizontalScrollIndicator = NO;
	myScrollView.showsVerticalScrollIndicator = NO;
	myScrollView.scrollsToTop = NO;
	myScrollView.delegate = self;
	
	myPageControl.numberOfPages = 5;
	myPageControl.currentPage = 0;
   // [self updateDots];

    [_scrollView setContentSize:CGSizeMake(320*3, _scrollView.frame.size.height)];
    _scrollView.pagingEnabled = YES;
    
    for (int i =0; i<5; i++) {
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*myScrollView.frame.size.width, 0, myScrollView.frame.size.width, myScrollView.frame.size.height)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"hospital%d.jpg",i+1]];
        imageView.tag = i+1;
        imageView.clipsToBounds = YES;
        [imageView setContentMode:UIViewContentModeScaleAspectFill];
        if (i == 0) {
            [imageView setContentMode:UIViewContentModeScaleToFill];
        }
        [myScrollView addSubview:imageView];
        [imageView release];
        
        
    }
    
   
    
    myTimer =  [NSTimer
                scheduledTimerWithTimeInterval:2
                target:self
                selector:@selector(updatePicture:)
                userInfo:nil
                repeats:YES];

    
   
    
}

- (void) updateDots

{
    
    NSArray *subView = myPageControl.subviews;
    
    for (int i =0; i < [subView count]; i++)
        
    {
        
        UIImageView *dot = (UIImageView *)[subView objectAtIndex:i];
        
        if (i == myPageControl.currentPage)
            dot.image = [UIImage imageNamed:@"dian_2.png"];
        
        else dot.image = [UIImage imageNamed:@"dian_1.png"];
        
        
    }
    
}



- (void)updatePicture:(id)sender{
    
    scrollIndex++;
    if (scrollIndex > 5) {
        scrollIndex = 0;
        
               
          [myScrollView scrollRectToVisible:CGRectMake(scrollIndex*320, 0, myScrollView.frame.size.width, myScrollView.frame.size.height) animated:NO];
        
        return;
    }
    
    [myScrollView scrollRectToVisible:CGRectMake(scrollIndex*320, 0, myScrollView.frame.size.width, myScrollView.frame.size.height) animated:YES];
    
    
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView == myScrollView) {
        
        CGFloat pageWidth = scrollView.frame.size.width;
        
        
        int _page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        
        myPageControl.currentPage = _page;
        //[self updateDots];

        
    }
    
       
      
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark --
#pragma mark view navigate  client

- (IBAction)upLoadData:(id)sender{
    
    self.navigationController.navigationBarHidden = NO;

    
    UploadDataViewController *contr = [[UploadDataViewController alloc]init];
    [self.navigationController pushViewController:contr animated:YES];
    [contr release];
}


- (IBAction)historyView:(id)sender{
    
     self.navigationController.navigationBarHidden = NO;
    ForumMainViewViewController *contr = [[ForumMainViewViewController alloc]init];
    [self.navigationController pushViewController:contr animated:YES];
    [contr release];
    
}

- (IBAction)blogView:(id)sender{
     self.navigationController.navigationBarHidden = NO;
    BlogMainViewViewController *contr = [[BlogMainViewViewController alloc]init];
    [self.navigationController pushViewController:contr animated:YES];
    [contr release];
}

- (IBAction)doctorMessageView:(id)sender{
     self.navigationController.navigationBarHidden = NO;
    ForumPostDetailViewController *contr = [[ForumPostDetailViewController alloc]init];
    [self.navigationController pushViewController:contr animated:YES];
    [contr release];

}

- (IBAction)settingView:(id)sender{
     self.navigationController.navigationBarHidden = NO;
    SettingViewController *contr = [[SettingViewController alloc]init];
    [self.navigationController pushViewController:contr animated:YES];
    [contr release];
}

- (IBAction)weekKnowledge:(id)sender{
    
    /*
    ITTCalDay *calDay = [[ITTCalDay alloc] initWithDate:[NSDate dateWithTimeIntervalSinceNow:2*24*60*60]];
    [calDay release];
    ITTCalMonth *calMonth = [[ITTCalMonth alloc] initWithMonth:4 year:2012];
    [calMonth release];
    
    self.navigationController.navigationBarHidden = NO;
    CalendarViewController *contr = [[CalendarViewController alloc]init];
    [self.navigationController pushViewController:contr animated:YES];
    [contr release];
  */
    
    self.navigationController.navigationBarHidden = NO;

    PregnancyKwoledgeViewController *viewContro = [[PregnancyKwoledgeViewController alloc]init];
    [self.navigationController pushViewController:viewContro animated:YES];
    [viewContro release];
    
    
    
}


- (IBAction)chartView:(id)sender{
    
    self.navigationController.navigationBarHidden = NO;
    ChartViewController *contr = [[ChartViewController alloc]init];
    [self.navigationController pushViewController:contr animated:YES];
    [contr release];
    
    
}

- (IBAction)circleView:(id)sender{
    
    
    UINavigationController *nav = [[[NSBundle mainBundle] loadNibNamed:@"CustomNavigationController" owner:self options:nil] objectAtIndex:0];
    [nav setViewControllers:[NSArray arrayWithObject:[[CircleViewController alloc] initWithNibName:@"CircleViewController" bundle:nil]]];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
   

    
}

#pragma mark --
#pragma mark view navigate  社区医院

- (IBAction)unreadOrderView:(id)sender{
    
    self.navigationController.navigationBarHidden = NO;
    UnreadOrdersViewController *contr = [[UnreadOrdersViewController alloc]init];
    [self.navigationController pushViewController:contr animated:YES];
    [contr release];
    
    
}
- (IBAction)havereadOrderView:(id)sender{
    
    self.navigationController.navigationBarHidden = NO;
    HaveReadOrdersViewController *contr = [[HaveReadOrdersViewController alloc]init];
    [self.navigationController pushViewController:contr animated:YES];
    [contr release];
}

- (IBAction)transferTocenterView:(id)sender{
    
    self.navigationController.navigationBarHidden = NO;
    TransferToCenterViewController *contr = [[TransferToCenterViewController alloc]init];
    [self.navigationController pushViewController:contr animated:YES];
    [contr release];
}

- (IBAction)to3DimageView:(id)sender{
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"meishi://"]];
}


@end
